//
//  SupplyChainVC.m
//  HappyStore
//
//  Created by 李小辉 on 16/11/21.
//  Copyright © 2016年 LLJ. All rights reserved.
//

#import "SupplyChainVC.h"
#import "SupplyChainView.h"
#import "SupplyChainProjectDetailsView.h"
#import "ProjectPublisherView.h"
#import "annexView.h"
#import "headsView.h"
#import "thirdPersonVC.h"
#import "OnlineQuotes.h"
@interface SupplyChainVC ()<UIWebViewDelegate>

@property (strong, nonatomic) SupplyChainView *supplyChainView;
@property (strong, nonatomic) SupplyChainProjectDetailsView *detailsView;
@property (strong, nonatomic) ProjectPublisherView *projectPublisherView;
@property (strong, nonatomic) annexView *annexview;
@property (strong, nonatomic) headsView *headsview;
@property (strong, nonatomic) UIButton *baoJiaBtn;
@property (assign, nonatomic) NSInteger count;
@property (assign, nonatomic) CGFloat webHeight;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollVIew;

@property (strong, nonatomic) NSDictionary *dataDic;//数据

@end


@implementation SupplyChainVC

- (SupplyChainView *)supplyChainView{
    if (!_supplyChainView) {
        _supplyChainView = [[[NSBundle mainBundle] loadNibNamed:@"SupplyChainView" owner:self options:nil] lastObject];
    }
    return _supplyChainView;
}

- (SupplyChainProjectDetailsView *)detailsView{
    if (!_detailsView) {
        _detailsView = [[[NSBundle mainBundle] loadNibNamed:@"SupplyChainProjectDetailsView" owner:self options:nil] lastObject];
    }
    return _detailsView;
}
- (ProjectPublisherView *)projectPublisherView{
    if (!_projectPublisherView) {
        _projectPublisherView = [[[NSBundle mainBundle] loadNibNamed:@"ProjectPublisherView" owner:self options:nil] lastObject];
    }
    return _projectPublisherView;
}
- (annexView *)annexview{
    if (!_annexview) {
        _annexview = [[[NSBundle mainBundle] loadNibNamed:@"annexView" owner:self options:nil] lastObject];
    }
    return _annexview;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
    //找出导航栏的下边的黑线
    UIImageView *navBarHairlineImageView = [NaviBarLine findHairlineImageViewUnder:self.navigationController.navigationBar];
    navBarHairlineImageView.hidden = NO;
    //导航条都透明度
    self.navigationController.navigationBar.translucent = NO;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"白色背景"] forBarMetrics:UIBarMetricsDefault];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    //导航条都透明度
    self.navigationController.navigationBar.translucent = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置导航栏title的字体大小等
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:19],NSFontAttributeName,RGB(45, 45, 45),NSForegroundColorAttributeName, nil]];
    
    // 设置返回按钮
    UIBarButtonItem *leftItem = [UIBarButtonItem itemWithTarget:self action:@selector(leftBarButtonItemClick) image:@"返回" highImage:@"返回"];
    self.navigationItem.leftBarButtonItem = leftItem;
    if (self.typeId == 2) {
        self.title = @"采购详情";
    }else{
        self.title = @"供应详情";
    }
    self.count = 1;
    self.scrollVIew.backgroundColor = RGB(247, 247, 247);
    [self startNetWork];
    
}
//返回
- (void)leftBarButtonItemClick{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - 配置视图
- (void)setupView{
//基础信息
    self.supplyChainView.titleLabel.text = self.dataDic[@"title"];
    CGSize titleSize = [self.supplyChainView.titleLabel sizeThatFits:CGSizeMake(screenW - 16, MAXFLOAT)];
    
    self.supplyChainView.industryLabel.text = self.dataDic[@"industry"];
    self.supplyChainView.adressLabel.text = self.dataDic[@"address"];
    self.supplyChainView.purchasePriceLabel.text = self.dataDic[@"money"];
    self.supplyChainView.purchaseQuantityLabel.text = self.dataDic[@"number"];
    self.supplyChainView.unitLabel.text = self.dataDic[@"unit"];
    self.supplyChainView.stopTime.text = self.dataDic[@"endTime"];
    self.supplyChainView.looksLabel.text = self.dataDic[@"browse_count"];
    [self.supplyChainView setFrame:CGRectMake(0, 0, self.supplyChainView.width, self.supplyChainView.titleLabel.y + titleSize.height + 226)];
    [self.scrollVIew addSubview:self.supplyChainView];
#pragma mark - 发布人
    [self.projectPublisherView.headPhotoImageView sd_setImageWithURL:[NSURL URLWithString:self.dataDic[@"headPhoto"]]];
    if ([self.dataDic[@"username"] isEqualToString:@""]||[self.dataDic[@"username"] isEqualToString:@"null"]) {
        self.projectPublisherView.userName.text = self.dataDic[@"account"];
    }else{
        self.projectPublisherView.userName.text = self.dataDic[@"username"];
    }
    
    if (self.dataDic[@"posiTionName"]==nil||[self.dataDic[@"posiTionName"] isEqualToString:@"null"]) {
        self.projectPublisherView.positionName.text = @"";
    }else{
        self.projectPublisherView.positionName.text = self.dataDic[@"posiTionName"];
    }
    //认证
    self.projectPublisherView.isAuthState.hidden = [UsedTool authStateImageViewHidden:self.dataDic[@"authState"]];
    //会员图标
    self.projectPublisherView.authStateImageView.image = [UsedTool setVipimage:self.dataDic[@"vipLevel"]];
    
    
    
    //封装判断好的上级
    self.projectPublisherView.addressLabel.text = [UsedTool userType:self.dataDic[@"userType"] companyName:self.dataDic[@"companyName"] userAddress:self.dataDic[@"userAddress"]];
    [self.projectPublisherView.jumpButton addTarget:self action:@selector(goGoveBy) forControlEvents:UIControlEventTouchUpInside];
    [self.projectPublisherView setFrame:CGRectMake(0, self.supplyChainView.height, self.projectPublisherView.width, self.projectPublisherView.height)];
    [self.scrollVIew addSubview:self.projectPublisherView];
    
//详情信息
#pragma mark - 详情信息
    //webview
    self.detailsView.descriptionWebView.delegate = self;
    self.detailsView.descriptionWebView.scrollView.scrollEnabled = NO;
    
    NSURL *url = [NSURL URLWithString:self.dataDic[@"content"]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.detailsView.descriptionWebView loadRequest:request];
    

    [self.detailsView setFrame:CGRectMake(0, self.projectPublisherView.y + self.projectPublisherView.height, self.detailsView.width, self.detailsView.descriptionWebView.y + self.detailsView.webVIewHeightLayoutConstraint.constant + 40)];
    [self.detailsView.clickBtn addTarget:self action:@selector(downButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollVIew addSubview:self.detailsView];
    
    
#pragma mark -附件
//    if ([self.dataDic[@"enclosureNum"] integerValue] == 0) {
        [self.annexview setFrame:CGRectMake(0, self.detailsView.y+self.detailsView.height +8, self.annexview.width, 0)];
//    }else{
//        [self.annexview setFrame:CGRectMake(0, self.detailsView.y+self.detailsView.height +8, self.annexview.width, self.annexview.height)];
//    }
    self.annexview.clipsToBounds = YES;
    [self.scrollVIew addSubview:self.annexview];
    //报价
#pragma mark - 报价头像
    //从我的点进来
    if ([self.tagStr isEqualToString:@"我的"]){
        
        self.scrollVIew.contentSize = CGSizeMake(screenW, self.annexview.y +self.annexview.height + 12);
    }else{

        self.headsview = [[headsView alloc] init];
        if ([self.dataDic[@"offerNumber"] integerValue] == 0) {
            [self.headsview setFrame:CGRectMake(0, self.annexview.y+self.annexview.height + 8, self.headsview.width, 0)];
        }else{
            //        self.headsview.manyStr = self.dataDic[@"offerNumber"];
            //超出5个取5个
            NSMutableArray *ary = [NSMutableArray array];
            if ([self.dataDic[@"offerNumber"] integerValue] > 5) {
                for (int i = 0; i < 5; i++) {
                    [ary addObject:self.dataDic[@"offer"][i]];
                }
            }else{
                ary = [NSMutableArray arrayWithArray:self.dataDic[@"offer"]];
            }
            
            [self.headsview initWithManyStr:self.dataDic[@"offerNumber"] HeadImageAry:ary];
            [self.headsview setFrame:CGRectMake(0, self.annexview.y+self.annexview.height + 8, self.headsview.width, self.headsview.height)];
        }
        [self.scrollVIew addSubview:self.headsview];
        //在线报价按钮
#pragma mark - 报价/订购按钮
        self.baoJiaBtn = [[UIButton alloc] initWithFrame:CGRectMake(40, self.headsview.y+self.headsview.height+24, screenW - 80, 35)];
        self.baoJiaBtn.layer.cornerRadius = 5.0f;
        self.baoJiaBtn.layer.masksToBounds = YES;
        [self.baoJiaBtn setBackgroundColor:RGB(0, 175, 220)];
        [self.baoJiaBtn setTintColor:[UIColor whiteColor]];
        if (self.typeId == 2) {
            [self.baoJiaBtn setTitle:@"在线报价" forState:UIControlStateNormal];
        }else{
            [self.baoJiaBtn setTitle:@"立即订购" forState:UIControlStateNormal];
        }
        [self.baoJiaBtn addTarget:self action:@selector(clickBaoJiaBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self.scrollVIew addSubview:self.baoJiaBtn];
        
        self.scrollVIew.contentSize = CGSizeMake(screenW, self.baoJiaBtn.y +self.baoJiaBtn.height + 12);
    }
}
//收起展开
-(void)downButton:(UIButton *)btn{
    btn.selected = !btn.selected;
    self.count +=1;
    if (self.count % 2 == 0) {
        self.detailsView.webVIewHeightLayoutConstraint.constant = self.webHeight;
    }else{
        self.detailsView.webVIewHeightLayoutConstraint.constant = 100;
    }
    [self setupView];
}
#pragma mark - 网页加载完成
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    self.webHeight = webView.scrollView.contentSize.height;
}
-(void)goGoveBy{
    if ([self.dataDic[@"userType"] integerValue] == 3) {
        goveDetailVC * goveVC = [[goveDetailVC alloc]init];
        goveVC.userId = self.dataDic[@"userId"];
        goveVC.userType = self.dataDic[@"userType"];
        [self.navigationController pushViewController:goveVC animated:YES];
    }else{
        thirdPersonVC *thirdVC = [[thirdPersonVC alloc] init];
        thirdVC.userId = self.dataDic[@"userId"];
        thirdVC.userType = self.dataDic[@"userType"];
        [self.navigationController pushViewController:thirdVC animated:YES];
    }
}

//在线报价or立即订购
- (void)clickBaoJiaBtn:(UIButton *)btn{
        OnlineQuotes *onlineQuotesVC = [[OnlineQuotes alloc] init];
    if ([btn.titleLabel.text isEqualToString:@"在线报价"]) {
        onlineQuotesVC.typeStr = @"2";
    }else{
        onlineQuotesVC.typeStr = @"1";
    }
    onlineQuotesVC.dataDic = self.dataDic;
    [self.navigationController pushViewController:onlineQuotesVC animated:YES];
}
#pragma mark - 网络请求
- (void)startNetWork{
//    2. 得到供应采购标详细信息
//http://192.168.1.188:8080/lensum/getSupplyDetail_app_chain.im?id=1
//    参数说明：id=供应采购id
    NSMutableDictionary *muDic = [NSMutableDictionary dictionary];
    muDic[@"id"] = self.resId;
    NSString *urlStr = @"getSupplyDetail_app_chain.im";
    [LSHttpTool sendHttpData:urlStr params:muDic success:^(id responseObject) {
        NSDictionary *dicInfo = [NSDictionary dictionaryWithDictionary:responseObject];
        if ([dicInfo[@"result"] intValue] == 1) {
            self.dataDic = [NSDictionary dictionaryWithDictionary:dicInfo[@"data"]];
            [self setupView];
        }
        
    } failure:^(NSError *error) {

        [self showSingleIconBubble:@"未搜索到资源"];
    }];
}

@end
